<template>
  <div class="userManager-box">
    <el-row class="btn-group">
      <el-col :offset="2" :span="4"><el-button type="primary" @click="findRole">新增</el-button></el-col>
      <el-col :span="4"><el-button type="danger" @click="bathdeleteShoes">批量删除</el-button></el-col>
      <el-col :span="9"><el-input v-model="name" prefix-icon="el-icon-search" placeholder="请输入搜索的商品"></el-input><el-button type="primary" @click="reachUser">搜索</el-button></el-col>
    </el-row>
    <el-row class="userManager-content">
      <el-table :data="shoeslist" @selection-change="getIds">
        <el-table-column type="selection"></el-table-column>
        <el-table-column width="120px">
          <template slot-scope="scope">
            <img :src="scope.row.shoes_img" class="shoes-img"/>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="shoes_name"></el-table-column>
        <el-table-column label="单价" prop="price" align="center"></el-table-column>
        <el-table-column label="商品详细信息" prop="memo" align="center"></el-table-column>
        <el-table-column label="种类" align="center">
          <template slot-scope="scope">
            <span>{{category[(scope.row.category_id)-1]}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="findOldShoes(scope.row.shoes_id)">修改</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row type="flex" justify="center">
      <el-pagination
            @size-change="pageSizeChange"
            @current-change="currPageChange"
            :current-page="curr_page"
            :page-sizes="[10, 50, 100, 500, 1000]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
    </el-row>
    <el-dialog
      title="新增商品"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form :rules="rules" ref="addgoods" :model="params" resetFields>
        <el-form-item label="商品名称">
          <el-input placeholder="请输入商品名称" v-model="params.shoes_name"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="price">
          <el-input placeholder="请输入商品单价" v-model="params.price"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible3">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="商品详情" prop="memo">
          <el-input type="textarea" placeholder="请输入商品内容" v-model="params.memo"></el-input>
        </el-form-item>
        <el-form-item label="商品种类">
          <template>
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addgoods('addgoods')">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改商品"
      :visible.sync="dialogVisible1"
      width="30%">
      <el-form :rules="rules" ref="updategoods" :model="params" resetFields>
        <el-form-item label="商品名称">
          <el-input placeholder="请输入商品名称" v-model="params.shoes_name"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" prop="price">
          <el-input placeholder="请输入商品单价" v-model="params.price"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            :data="uploadData"
            action="shoes_Web_exploded/Shoes/updateShoes"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible3">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="商品详情" prop="memo">
          <el-input type="textarea" placeholder="请输入商品内容" v-model="params.memo"></el-input>
        </el-form-item>
        <el-form-item label="商品种类">
          <template>
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="updateshoesgoods('updategoods')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import http from '@/utils/HttpUtil'
export default {
  data () {
    return {
      rules: {
        name: [
          {
            required: true, message: '商品名称不能为空', trigger: 'blur'
          }],
        price: [
          {
            required: true, message: '商品单价不能为空', trigger: 'blur'
          }],
        memo: [
          {
            required: true, message: '商品详情不能为空', trigger: 'blur'
          }]
      },
      action: {
        selectshoes: 'shoes_Web_exploded/Shoes/selectShoesByName',
        addshoes: 'shoes_Web_exploded/Shoes/addShoes',
        deletedshoes: 'shoes_Web_exploded/Shoes/deletedShoes',
        findshoes: 'shoes_Web_exploded/Shoes/selectShoesById',
        updateshoes: 'shoes_Web_exploded/Shoes/updateShoes',
        updateshoess: 'shoes_Web_exploded/Shoes/updateShoess'
      },
      dialogImageUrl: '',
      dialogVisible3: false,
      params: {
        shoes_id: null,
        price: '',
        shoes_name: '',
        shoes_img: '',
        category_id: '',
        memo: ''
      },
      uploadData: {
        shoes_id: null
      },
      total: 0,
      page_size: 10,
      curr_page: 1,
      shoeslist: [],
      name: '',
      dialogVisible: false,
      dialogVisible1: false,
      category: ['休闲鞋', '跑鞋', '篮球鞋'],
      options: [{
        value: '0',
        label: '休闲鞋'
      }, {
        value: '1',
        label: '跑鞋'
      }, {
        value: '2',
        label: '篮球鞋'
      }],
      value: ''
    }
  },
  components: {

  },
  computed: {

  },
  methods: {
    reachUser () {
      this.getGoodsList(this.name, 1, this.pageSize)
    },
    pageSizeChange (pageSize) {
      this.getGoodsList(this.name, 1, pageSize)
    },
    currPageChange (currPage) {
      this.getGoodsList(this.name, currPage, this.page_size)
    },
    getGoodsList (name = null, curr_page = 1, page_size = 10) {
      const params = {
        'curr_page': curr_page,
        'page_size': page_size
      }
      if (name !== null && name !== '') {
        params['shoes_name'] = name
      }
      http.xhrPost(this, this.action.selectshoes, params, (res) => {
        this.shoeslist = res.data.list
        this.page_size = res.data.pageSize
        this.curr_page = res.data.pageNum
        this.total = res.data.total
      })
    },
    addgoods (refName) {
      this.$refs[refName].validate((valid) => {
        let params = this.params
        http.xhrPost(this, this.action.addshoes, params, (res) => {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.dialogVisible = false
          this.getGoodsList()
          this.params = {
            shoes_id: null,
            price: '',
            shoes_name: '',
            shoes_img: '',
            category_id: '',
            memo: ''
          }
        })
      })
    },
    getIds (shoeslist) {
      // 清楚上次选中的id
      this.ids = ''
      shoeslist.forEach((item, index) => {
        this.ids += item.shoes_id + ','
      })
    },
    bathdeleteShoes () {
      if (this.ids !== '') {
        this.$confirm('您正在执行批量删除操作', '提示', {
          confirmButtonText: '残忍丢弃',
          cancelButtonText: '容我想想',
          type: 'warning'
        }).then(() => {
          let Ids = this.ids.substring(0, this.ids.length - 1)
          this.deleted(Ids)
        })
      } else {
        this.$message({
          type: 'error',
          message: '请至少勾选一条数据'
        })
      }
    },
    deleted (Ids) {
      http.xhrPost(this, this.action.deletedshoes + '/' + Ids, null, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getGoodsList(this.name, this.curr_page, this.page_size)
      }, 'delete')
    },
    // 用户回显数据
    findOldShoes (id) {
      this.dialogVisible1 = true
      this.uploadData.shoes_id = id
      http.xhrPost(this, this.action.findshoes + '/' + id, null, (res) => {
        this.params.shoes_id = res.data.shoes_id
        this.params.price = res.data.price
        this.params.shoes_img = res.data.shoes_img
        this.params.shoes_name = res.data.shoes_name
        this.value = res.data.category_id
        this.params.memo = res.data.memo
      })
    },
    findRole () {
      this.dialogVisible = true
      this.params = {
        shoes_id: null,
        price: '',
        shoes_name: '',
        shoes_img: '',
        category_id: '',
        memo: ''
      }
    },
    updateshoesgoods (refName) {
      this.$refs[refName].validate((valid) => {
        if (valid) {
          let params = this.params
          params['category_id'] = this.value
          http.xhrPost(this, this.action.updateshoess, params, (res) => {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.params = {
              shoes_id: null,
              price: '',
              shoes_name: '',
              category_id: '',
              memo: ''
            }
            this.dialogVisible1 = false
            this.getGoodsList(this.name, this.curr_page, this.page_size)
          })
        }
      })
    },
    handleRemove (file) {
      console.log(file)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload (file) {
      console.log(file)
    }
  },
  mounted () {
    this.getGoodsList()
  }
}
</script>

<style lang="scss">
@import '../../assets/style/common.scss';
  .userManager-box {
    margin: 0px;
    padding: 0px;
    .btn-group {
      .el-input {
        width: 250px;
      }
    }
    .userManager-content {
      margin-top: 20px;
      .shoes-img {
        width: 100px;
        height: 100px;
      }
    }
  }
</style>
